package com.qys.livesMall.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.framework.view.BaseQuery;
import com.qys.livesMall.system.enums.SystemMessageEnum;
import com.qys.livesMall.system.model.dto.sys.PlatformNewsDto;
import com.qys.livesMall.system.model.entity.PlatformNews;
import com.qys.livesMall.system.mapper.PlatformNewsMapper;
import com.qys.livesMall.system.model.vo.sys.PlatformNewsVO;
import com.qys.livesMall.system.service.PlatformNewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jodd.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 平台新闻发布 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-08-29
 */
@Service
public class PlatformNewsServiceImpl extends ServiceImpl<PlatformNewsMapper, PlatformNews> implements PlatformNewsService {

    @Override
    public boolean add(PlatformNewsDto dto) {
        PlatformNews platformNews=new PlatformNews();
        BeanUtil.copyProperties(dto,platformNews);
        platformNews.setId(null);
        platformNews.setCreateBy(SecurityUtils.getUsername());
        platformNews.setCreateTime(LocalDateTime.now());
        save(platformNews);
        return true;
    }

    @Override
    public boolean updateInfo(PlatformNewsDto dto) {
        PlatformNews platformNews=getById(dto.getId());
        if (null == platformNews){
            throw new QysException(SystemMessageEnum.SYSTEM_NOT_ID);
        }
        BeanUtil.copyProperties(dto,platformNews);
        platformNews.setUpdateBy(SecurityUtils.getUsername());
        platformNews.setUpdateTime(LocalDateTime.now());
        updateById(platformNews);
        return true;
    }

    @Override
    public IPage<PlatformNewsVO> query(BaseQuery dto) {
        LambdaQueryWrapper<PlatformNews> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PlatformNews::getDeleted, 0);
        queryWrapper.and(StringUtil.isNotBlank(dto.getName()), wrapper->{
            wrapper.like(PlatformNews::getTitle, dto.getName());
            wrapper.or().like(PlatformNews::getContent, dto.getName());
        });
        queryWrapper.orderByDesc(PlatformNews::getCreateTime);
        IPage<PlatformNewsVO> page=page(new Page<>(dto.getPageNum(), dto.getPageSize()), queryWrapper).convert(e -> {
            PlatformNewsVO platformNewsVO=new PlatformNewsVO();
            BeanUtils.copyProperties(e,platformNewsVO);
            platformNewsVO.setCreatelongtime(e.getCreateTime().toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
            return platformNewsVO;
        });
        return page;
    }

    @Override
    public PlatformNewsVO info(Long id) {
        PlatformNews platformNews=getById(id);
        if (null == platformNews){
            throw new QysException(SystemMessageEnum.SYSTEM_NOT_ID);
        }
        PlatformNewsVO platformNewsVO=new PlatformNewsVO();
        BeanUtils.copyProperties(platformNews,platformNewsVO);
        return platformNewsVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateState(PlatformNewsDto dto) {
        PlatformNews platformNews=getById(dto.getId());
        platformNews.setState(dto.getState());
        platformNews.setUpdateBy(SecurityUtils.getUsername());
        platformNews.setUpdateTime(LocalDateTime.now());
        updateById(platformNews);
        return true;
    }

    @Override
    public List<PlatformNewsVO> getInfo() {
        LambdaQueryWrapper<PlatformNews> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PlatformNews::getDeleted, 0);
        queryWrapper.eq(PlatformNews::getState,1);
        queryWrapper.orderByDesc(PlatformNews::getState);
        List<PlatformNews> list=list(queryWrapper);
        List<PlatformNewsVO> platformNewsVOS=new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)){
            for (PlatformNews bean:list){
                PlatformNewsVO platformNewsVO=new PlatformNewsVO();
                BeanUtils.copyProperties(bean,platformNewsVO);
                platformNewsVOS.add(platformNewsVO);
            }
        }
        return platformNewsVOS;
    }
}
